package com.example.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;

/**
 * 学生实体类
 * 对应数据库表: tb_student
 * 
 * @author Simon
 * @since 2025-09-09 19:30
 */
@Data 
@TableName("tb_student")
@Schema(name = "Student", description = "学生信息")
public class Student {
    
    /**
     * 主键，自增
     */
    @Schema(description = "学生ID", example = "1")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    
    /**
     * 学号，唯一
     * MyBatis‑Plus/ MyBatis 默认会启用驼峰映射（studentNo ↔ student_no），符合规则时无需 @TableField 显式指定
     * 只有在列名与属性名不按驼峰规则对应、或需要设置其它属性（如 exist=false、typeHandler 等），或你在配置中关闭了驼峰映射，才需要加注解
     */
    @Schema(description = "学号", example = "12", required = true)
    @TableField("student_no")
    private Integer studentNo;
    
    /**
     * 名字
     */
    @Schema(description = "名字", example = "小明", required = true)
    @TableField("first_name")
    private String firstName;

    /**
     * 姓氏
     */
    @Schema(description = "姓氏", example = "张", required = true)
    @TableField("last_name")
    private String lastName;

    /**
     * 全名（业务字段，组装 lastName + firstName）
     * exist = false 表示数据表中没有这个字段，不参与数据表的映射
     * 
     */
    @Schema(description = "全名（姓氏+名字）", example = "张小明", accessMode = Schema.AccessMode.READ_ONLY)
    @TableField(exist = false)
    private String fullName;
    
    /**
     * 获取全名：lastName + firstName
     * @return 组装后的全名
     * 
     * 由于使用了Lombok的 @Data 注解，会自动调用所有的getter方法，所以查询时，会自动调用getFullName方法
     */
    public String getFullName() {
        if (lastName != null && firstName != null) {
            return lastName + firstName;
        }
        return fullName;
    }
    
    /**
     * 性别 M/F
     */
    @Schema(description = "性别", example = "M", allowableValues = {"M", "F"})
    @TableField("gender")
    private String gender;

    public String getGender() {
        if ("M".equals(gender)) {
            return "男";
        } else if ("F".equals(gender)) {
            return "女";
        }
        return gender;
    }
    
    /**
     * 年龄
     */
    @Schema(description = "年龄", example = "20", minimum = "1", maximum = "150")
    @TableField("age")
    private Integer age;
    
    /**
     * 出生日期
     */
    @Schema(description = "出生日期", example = "2003-05-15", format = "date")
    @TableField("birthday")
    private Date birthday;
    
    /**
     * 获取生日
     * @return 格式化后的日期字符串，如：2018-11-15
     * 
     * 尽管birthday为Date类型，但由于我们自定义了getter方法，则返回值类型以getter为准
     * 如果我们没有自定义getter，Lombok会自动为每个字段生成getter，则返回值类型仍然是Date
     */
    public String getBirthday() {
        if (birthday != null) {
            // 先转换为本地时区的Date对象
            Date localDate = Date.from(birthday.toInstant().atZone(ZoneId.systemDefault()).toInstant());
            
            // 再格式化为年-月-日格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(localDate);
        }
        return null;
    }

    /**
     * 年级ID
     */
    @Schema(description = "年级ID", example = "1")
    @TableField("grade_id")
    private Long gradeId;

    /**
     * 班级ID
     */
    @Schema(description = "班级ID", example = "1")
    @TableField("class_id")
    private Long classId;
    
    /**
     * 电话
     */
    @Schema(description = "联系电话", example = "13800138000", pattern = "^1[3-9]\\d{9}$")
    @TableField("phone")
    private String phone;
    
    /**
     * 邮箱
     */
    @Schema(description = "邮箱地址", example = "zhangxiaoming@example.com", format = "email")
    @TableField("email")
    private String email;
    
    /**
     * 家庭住址
     */
    @Schema(description = "家庭住址", example = "北京市朝阳区某某街道123号")
    @TableField("address")
    private String address;
    
    /**
     * 头像URL
     */
    @Schema(description = "头像链接", example = "https://example.com/avatar.jpg", format = "uri")
    @TableField("avatar_url")
    private String avatarUrl;
    
    /**
     * 状态 0=正常 1=禁用
     */
    @Schema(description = "学生状态", example = "0", allowableValues = {"0", "1"})
    @TableField("status")
    private Integer status;
    
    /**
     * 创建时间
     */
    @Schema(description = "创建时间", example = "2025-09-11T10:30:00", accessMode = Schema.AccessMode.READ_ONLY)
    @TableField("create_time")
    private Date createTime;
    
    /**
     * 更新时间
     */
    @Schema(description = "更新时间", example = "2025-09-11T15:45:30", accessMode = Schema.AccessMode.READ_ONLY)
    @TableField("update_time")
    private Date updateTime;
    
    /**
     * 备注
     */
    @Schema(description = "备注信息", example = "三好学生，成绩优秀", maxLength = 500)
    @TableField("remark")
    private String remark;
}
